<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="js/vue.min.js"></script>
    <script src="js/element.js"></script>
    <style>
        body, html {
            height: 100%;
            margin: 0;
            overflow-x: hidden;
        }

        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background-image: url('img/bg.jpg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }

        #box {
            width: 500px;
            height: 620px;
            background: white;
            border-radius: 5px;
            margin: 100px auto;
        }
        .demo-ruleForm{
            width: 400px;
            padding-top: 50px;
        }

    </style>
    <link rel="stylesheet" href="css/element.css">
</head>
<body>
<!--背景图-->
<div class="background"></div>
<div id="box">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="邮箱" prop="email">
            <el-input type="email" v-model="ruleForm.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button :plain="false" type="primary" @click="open" >发送邮箱
                {{ruleForm.myTime>60?"":ruleForm.myTime}}
            </el-button>
        </el-form-item>

        <el-form-item label="验证码" prop="Code">
            <el-input type="text" v-model="ruleForm.Code" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="submitForm()">重置密码</el-button>
            <el-button @click="resetForm('ruleForm')">返回登陆</el-button>
        </el-form-item>

    </el-form>
</div>

<script>
    new Vue({
        el:"#box",
        data() {
            var validatePass = (rule, value, callback) => {
                var pattern = /^\d{6,}$/;
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else if (pattern.test(value)) {
                    callback(); // 验证通过
                } else {
                    callback(new Error('密码格式错误，至少6位数字'));
                }
            };

            var validateEmail2 = (rule, value, callback) => {
                // 使用更通用的邮箱正则表达式
                var pattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
                if (value === '') {
                    callback(new Error('请输入邮箱'));
                } else if (pattern.test(value)) {
                    callback(); // 验证通过
                } else {
                    callback(new Error('邮箱格式错误xxx@xx.xx'));
                }
            };

            // 验证码
            var validateCode = (rule, value, callback) => {
                // 使用更通用的邮箱正则表达式
                var pattern = /^\d{6}$/;
                if (value === '') {
                    callback(new Error('请输入验证码'));
                } else if (pattern.test(value)) {
                    callback(); // 验证通过
                } else {
                    callback(new Error('验证码错误,验证码必须六位及以上'));
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    email: '',
                    code:'',
                    //设置周期性定时器
                    myTime:"61"
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    email: [
                        { validator: validateEmail2, trigger: 'blur' }
                    ],
                    Code:[
                        { validator: validateCode, trigger: 'blur' }
                    ]
                }
            };
        },
        methods:{
            submitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        alert('密码重置成功！');
                        this.ruleForm.email = "";
                        this.ruleForm.pass = "";
                        this.ruleForm.Code = "";
                    } else {
                        console.log('密码重置失败');
                return false;
            }
            });
            },
            resetForm(formName) {
                document.location = "index.html";
            },
            //消息弹框
            open() {
                this.$message({
                    message: '验证码已发送',
                    type: 'success'
                });

                // 初始化时间为数值类型
                this.ruleForm.myTime = 60;
                // 保存 Vue 实例的引用
                var vm = this;

                // 使用箭头函数或者保存的引用
                const timer = setInterval(() => {
                    if (vm.ruleForm.myTime > 0) {
                    vm.ruleForm.myTime--;
                } else {
                    // 倒计时结束后清除计时器
                    clearInterval(timer);
                    vm.ruleForm.countdown = 61;
                }
            }, 100);
            },

        }

    })
</script>
</body>
</html>